package com.sos.core
{
	import com.sos.Shared;
	import com.sos.core.DataObject;
	import com.sos.logging.Dumper;
	import com.sos.logging.IDumpable;

	public class PlayerList extends DataObject implements IDumpable
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------
		
		// JSON Property names
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _list:Vector.<Player>;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTOR
		// -------------------------------------------------------------------------------
		
		public function PlayerList(data:Object = null)
		{
			// Init objects
			_list = new Vector.<Player>();
			// Clear
			clear();
			// If data is provided, initializes the instance
			if (data != null) load(data);
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------
		
		public function get length():uint 
		{
			return _list.length;
		}
		
		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		private function indexOfById(id:uint):int
		{
			var index:int = Shared.INVALID_INDEX;
			for (var i:int = 0; i < _list.length; i++)
			{
				if (_list[i].id == id) break;
			}
			return index;
		}
		
		// DataObject implementation
		
		override protected function validate(data:Object):void
		{
		}
		
		override protected function fill(data:Object):void
		{
			// Value parsing and casting
			var list:Vector.<Player> = new Vector.<Player>();
			// Objects parsing
			for (var i:int = 0; i < data.length; i++)
			{
				list.push(new Player(data[i]));
			}
			// Updating Game list
			_list.splice(0, _list.length);
			for (var j:int = 0; j < list.length; j++)
			{
				_list.push(list[j]);
			}			
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function addPlayer(item:Player):void
		{
			_list.push(item);
		}
		
		public function getPlayer(index:int):Player
		{
			return _list[index];
		}
		
		public function getPlayerById(id:uint):Player
		{
			var index:int = indexOfById(id);
			var player:Player = null;
			if (index != Shared.INVALID_INDEX) player = _list[index];
			return player;
		}
		
		public function copy(playerList:PlayerList):void
		{
			// Value parsing and casting
			var list:Vector.<Player> = new Vector.<Player>();
			// Objects parsing
			for (var i:int = 0; i < playerList.length; i++)
			{
				list.push(playerList[i].clone());
			}
			// Updating player list
			_list.splice(0, _list.length);
			for (var j:int = 0; j < list.length; j++)
			{
				_list.push(list[j]);
			}
		}
		
		public function clone():PlayerList
		{
			var gameList:PlayerList = new PlayerList();
			gameList.copy(this);
			return gameList;
		}
		
		override public function clear():void
		{
			_list.splice(0, _list.length);
		}
		
		public function dump(source:Array = null, indentDepth:int = 0):Array
		{
			return Dumper.dump(this, null, source, indentDepth);
		}
		
	}
}